﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Relocation.Base;
using Relocation.Classes;
using Relocation.Com;
using Relocation.Data;

namespace Relocation
{
	public partial class Statistics : Form, IPrintForm
	{
		public static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		public Session Session
		{
			get;
			set;
		}

		//统计开始日期和结束日期
		

		public Statistics()
		{
			InitializeComponent();
		}

		public Statistics(Session session)
			: this()
		{
			this.Session = session;
			this.statisticDataview.init();
			statistic();
		}

		//统计按钮
		private void statusBT_Click(object sender, EventArgs e)
		{
			try
			{
				statistic();
			} catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Error(ex.Message);
			}
		}

		private void statistic()
		{
			statisticDataview.Rows.Clear();
			DateTime? startTime = this.Field_S_StartBox.Value;
			DateTime? endTime = this.Field_S_EndBox.Value;
			
			string[] arryList;

			List<Projects> projects = Session.DataModel.Projects.ToList();
			foreach (Projects p in projects)
			{
				string pName = "";   //项目名称
				int total = 0;  //总户数

				int unPlacement_house = 0;  //未安置住宅户数
				int unPlacement_shop = 0;   //未安置门市户数
				int unPlacement_other = 0;  //未安置其它户数

				int isPlacement_house = 0;  //已安置住宅户数
				int isPlacement_shop = 0;   //已安置门市户数
				int isPlacement_other = 0;  //已安置其它户数


				int currency = 0;//货币补偿户数

				int sum = 0;    //户数
				int house_sum = 0;  //住宅房户数                
				int house_number = 0;   //住宅房套数
				int shop_sum = 0;   //门市房户数
				int shop_number = 0;    //门市房间数

				decimal currency_money = 0; //货币化补偿金额

				decimal house_area = 0; //住宅总拆迁面积
				decimal shop_area = 0;  //门市总拆迁面积
				decimal other_area = 0; //其它总拆迁面积
				decimal currency_area = 0;  //货币补偿总拆迁面积
				decimal area = 0;     //总拆迁面积

				pName = p.projectName;

				p.Relocatees.Load();
				
				IQueryable<Relocatees> iQueryable_Relocatees = p.Relocatees.AsQueryable();
				if (startTime.HasValue)
					iQueryable_Relocatees = iQueryable_Relocatees.Where(t => t.enableDate.HasValue&&t.enableDate.Value >= startTime.Value);
				if (endTime.HasValue)
					iQueryable_Relocatees = iQueryable_Relocatees.Where(t => t.enableDate.HasValue&& t.enableDate.Value <= endTime.Value);
				List<Relocatees> relocatees = iQueryable_Relocatees.ToList();					

				foreach (Relocatees r in relocatees)
				{
					if (r != null && r.statusFlag != null)
					{
						switch (Convert.ToInt32(r.statusFlag))
						{
							case 0:
								if (r.structure != null && r.structure.Trim() == "住宅")
								{
									unPlacement_house++;
									house_area += r.area == null ? 0 : (decimal)r.area;
								} else if (r.structure != null && r.structure.Trim() == "门市")
								{
									unPlacement_shop++;
									shop_area += r.area == null ? 0 : (decimal)r.area;
								} else
								{
									unPlacement_other++;
									other_area += r.area == null ? 0 : (decimal)r.area;
								}
								break;
							case 1:
								if (r.structure != null && r.structure.Trim() == "住宅")
								{
									isPlacement_house++;
									house_area += r.area == null ? 0 : (decimal)r.area;
								} else if (r.structure != null && r.structure.Trim() == "门市")
								{
									isPlacement_shop++;
									shop_area += r.area == null ? 0 : (decimal)r.area;
								} else
								{
									isPlacement_other++;
									other_area += r.area == null ? 0 : (decimal)r.area;
								}


								if (r.residence != null && (int)r.residence > 0)
								{
									house_sum++;
									house_number += (int)r.residence;
								}
								if (r.shop != null && (int)r.shop > 0)
								{
									shop_sum++;
									shop_number += (int)r.shop;
								}
								break;
							default:
								currency++;
								currency_money += r.payment == null ? 0 : (decimal)r.payment;
								currency_area += r.area == null ? 0 : (decimal)r.area;
								break;
						}
						//area += relocatees.area == null ? 0 : (decimal)relocatees.area;                 
					}
					total = unPlacement_house + unPlacement_shop + unPlacement_other + isPlacement_house + isPlacement_shop + isPlacement_other + currency;
					sum = house_sum + shop_sum;
					area = house_area + shop_area + other_area + currency_area;
				}

				arryList = new string[] { pName,total.ToString(),                     
					     unPlacement_house .ToString(),
					     unPlacement_shop.ToString(),
					     unPlacement_other.ToString(),
					     isPlacement_house.ToString(),
					     isPlacement_shop.ToString(),
					     isPlacement_other.ToString(),
					     currency.ToString(), 
					     sum.ToString(),
					     house_sum.ToString(),
					     house_number.ToString(),
					     shop_sum.ToString(),
					     shop_number.ToString(),
					     currency_money.ToString(), 
					     house_area.ToString(),
					     shop_area.ToString(),
					     other_area.ToString(),
					     currency_area.ToString(),
					     area.ToString() };
				statisticDataview.Rows.Add(arryList);
			}
		}

		#region IPrintForm 成员

		void IPrintForm.Print()
		{
			GridToExcel.print(this.statisticDataview);
		}

		#endregion
	}
}
